package com.atguigu.spzx.cart.service.Impl;

import com.atguigu.spzx.cart.service.CartService;
import com.atguigu.spzx.feign.product.ProductFeignClient;
import com.atguigu.spzx.model.entity.cart.CartInfo;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.exception.GuiguException;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.tools.SpzxConsts;
import com.atguigu.spzx.util.AuthContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LT
 * @create 2024-03-28-16:08
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ProductFeignClient productFeignClient;

    @Override
    public void addToCart(Long skuId, Integer skuNum) {
        Long userId = AuthContextUtil.get1().getId();
        BoundHashOperations<String, String, CartInfo> cart = redisTemplate.boundHashOps(SpzxConsts.SPZX_CART_PREFIX+userId);
        CartInfo cartInfo = cart.get(String.valueOf(skuId));
        if (cartInfo !=null){
            cartInfo.setSkuNum(cartInfo.getSkuNum()+skuNum);
            cartInfo.setUpdateTime(new Date());
        } else {
            cartInfo = new CartInfo();
            cartInfo.setSkuId(skuId);
            cartInfo.setSkuNum(skuNum);
            cartInfo.setUserId(userId);
            cartInfo.setIsChecked(1);
            cartInfo.setCreateTime(new Date());
            cartInfo.setUpdateTime(new Date());
            Result<ProductSku> result = productFeignClient.getBySkuId(skuId);
            if (result.getCode().intValue() != 200){
                throw new GuiguException(ResultCodeEnum.ERROR);
            }
            ProductSku productSku = result.getData();
            cartInfo.setCartPrice(productSku.getSalePrice());
            cartInfo.setImgUrl(productSku.getThumbImg());
            cartInfo.setSkuName(productSku.getSkuName());
        }
        cart.put(String.valueOf(skuId),cartInfo);

    }


    @Override
    public List<CartInfo> cartList() {
        BoundHashOperations<String, String, CartInfo> cart = getCart();
        return cart.values();
    }

    @Override
    public void deleteCart(Long skuId) {
        BoundHashOperations<String, String, CartInfo> cart = getCart();
        cart.delete(String.valueOf(skuId));
    }

    @Override
    public void clearCart() {
        Long userId = AuthContextUtil.get1().getId();
        redisTemplate.delete(SpzxConsts.SPZX_CART_PREFIX+userId);
    }

    @Override
    public void checkCart(Integer skuId, Integer isChecked) {
        BoundHashOperations<String, String, CartInfo> cart = getCart();
        cart.entries().forEach((skuIdStr,cartInfo)->{
            if (cartInfo.getSkuId().intValue() == skuId){
                cartInfo.setIsChecked(isChecked);
                cart.put(skuIdStr,cartInfo);
            }
        });
    }

    @Override
    public List<CartInfo> getAllCkecked() {
        BoundHashOperations<String, String, CartInfo> cart = getCart();
        List<CartInfo> cartInfos = cart.values().stream().filter(cartInfo -> cartInfo.getIsChecked().intValue() == 1)
                .collect(Collectors.toList());
        return cartInfos;
    }

    @Override
    public void allCheckCart(Integer isChecked) {
        BoundHashOperations<String, String, CartInfo> cart = getCart();
        cart.entries().forEach((skuIdStr,cartInfo)->{
            cartInfo.setIsChecked(isChecked);
            cart.put(skuIdStr,cartInfo);
        });
    }

    @Override
    public void clearCheckedCart() {

        BoundHashOperations<String, String, CartInfo> cart = getCart();
        cart.entries().forEach((skuIdStr,cartItem) ->{
            if (cartItem.getIsChecked()==1){
                cart.delete(skuIdStr);
            }
        });


    }

    public BoundHashOperations<String,String,CartInfo> getCart(){
        Long userId = AuthContextUtil.get1().getId();
        BoundHashOperations<String,String,CartInfo> boundHashOperations = redisTemplate.boundHashOps(SpzxConsts.SPZX_CART_PREFIX + userId);

        return boundHashOperations;
    }


}
